import API from '@/service/api'

function uploadByPercentage(file,fileList) {
    return new Promise((resolve, reject) =>{
      file.uid = Date.now()
      const _file = {
        status: 'uploading',
        name: file.name,
        size: file.size,
        percentage: 0,
        uid: file.uid,
        showProgress: true
      };
      fileList.push(_file)
      API.file.upLoad(file,p=>{
        let prog = parseInt(100 * ( p.loaded / p.total ))
        _file.percentage = prog
      }).then(res=>{
        _file.status = 'finished'
        _file.percentage = 100
        _file.url = res
        resolve(res)
        setTimeout(() => {
          _file.showProgress = false
        }, 1000)
      })
    })
}

export default uploadByPercentage